<template>
  <div class="user">
    <basic-container>
      <avue-crud
        :option="option"
        ref="crud"
        v-model="form"
        :page="page"
        :simple-page="true"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @search-change="handleFilter"
        @search-reset="handleRefreshChange"
        @refresh-change="handleRefreshChange"
        @row-save="create"
        @row-update="update"
        @row-del="deletes"
        :data="list"
      >
        <template slot-scope="scope" slot="num">
          <span @click="goApply(scope.row.id)" class="underline">{{scope.row.num}}</span>
        </template>
        <template slot="contentForm" slot-scope="scope">
          <quill-editor ref="text" v-model="form.content" class="myQuillEditor" :options="{}" />
        </template>
        <template slot="menu" slot-scope="scope">
          
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchActivityList,
  putActivity,
  deleteActivity
} from "@/api/admin/activity";
import { listOption } from "@/const/crud/admin/activity";
import { mapGetters } from "vuex";

import { quillEditor } from 'vue-quill-editor';
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

export default {
  name: "table_user",
  components: {
    quillEditor
  },
  data() {
    return {
      option: listOption,
      params: {},
      rechargeForm: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false //是否倒序
      },
      list: [],
      listLoading: true,
      role: [],
      form: {},
      searchDate: ''
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  watch: {
    role() {
      this.form.role = this.role;
    }
  },
  created() {
    this.getList(this.page, {});
    if(this.permissions["isCompany"]){
      // this.findObject(this.option.column, 'id').search = false
    }
  },
  methods: {
    getList(page, params) {
      this.listLoading = true;
      fetchActivityList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          params || this.params
        )
      ).then(response => {
        this.list = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.page.currentPage = 1;
      done();
    },
    handleRefreshChange() {
      this.searchDate = ''
      this.getList(this.page);
    },
    selectionChange(list) {
      this.selIds = (list || []).map(o => o.id).join(",");
    },
    goApply(id) {
      console.log(id)
      this.$router.push(`/admin/activity/apply?id=${id}`)
    },
    handleRecharge(row, index) {
      try {
        this.$refs.formrecharge.resetForm();
      }catch(e){}
      this.rechargeForm.company_id = row.id
      this.rechargeForm.company_name = row.company_name
      this.showRechargeDialog = true
    },
    create(form, done) {
      putActivity(form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
          this.listLoading = false
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      
      putActivity(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("修改成功");
        })
        .catch(() => {
          loading();
        });
    },
    uploadImgBefore(file, done, loading,column){
      if(file.type === 'image/jpeg' || file.type === 'image/png'){
        done()
        return true
      }else{
        this.$message.warning('请上传JPG图片');
        loading();
        return false
      }
    },
    deletes(row) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteActivity({ id: row.id })
          .then(() => {
            this.getList(this.page);
            this.$notify.success("删除成功");
          })
          .catch(() => {
            this.$notify.error("删除失败");
          });
      });
    }
  }
};
</script>

<style lang="less" scoped>
  .underline {
    text-decoration: underline;
    color: blue;
    cursor: pointer;
  }
</style>